﻿using COC.CComponent;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TileMapDemoV1 : MonoBehaviour
{

    #region collider profile

    private MeshFilter mf;
    private MeshCollider mc;
    private MeshRenderer mr;
	

    #endregion


    #region map parameters

        float tileSize = 1.0f;
        float xCount = 10;
        float yCount = 10;
        float xOffset = 0.02f;
        float yOffset = 0.02f;
        float nodeSize = 0.0f;
        int segmentWidthTileCount = 3;
        int segmentHeightTileCount = 2;
    #endregion

    	readonly object _lock = new object ();
		
		GameObject markNode = null;
		int direction = 0; //0=left 1=right 2=up 3=down

    #region map vertices container

        public bool isDEBUG = false;

    private Dictionary<Vector3, int> allTileNodes = new Dictionary<Vector3, int>();
        //cache all tile nodes when indicated a coordinate(x,y,z),and key that to which space is a available.o=available,1=unavailable

    private Vector3[] mapVertices;
    private GameObject buildModel = null;

    [HideInInspector] private CQuad<Vector3, Vector3, int, int>
        mMapPivotSpace = new CQuad<Vector3, Vector3, int, int>(); //deault unit space by target character 

    private CQuad<Vector3, Vector3, int, int>
        mPivotSpace = new CQuad<Vector3, Vector3, int, int>(); //deault unit space by target character 

    //Vector3[] direction = new Vector3[4];
    private CQuad<Vector3, Vector3, int, int> movePivotSpace = new CQuad<Vector3, Vector3, int, int>();

    #endregion

		GameObject[]  mAnchors = new GameObject[4];
	    GameObject go;
	    Rigidbody tempRigidBody=null;
		BoxCollider mBoxCollider=null;
		string modelType="infrastructure";
        Vector3 boxScale=new Vector3(1.0f,1.0f,1.0f);
        public static readonly string buildingCollideFlag = "BuildModel";
        public static readonly int buildingLayer = 8;
		void Awake ()
		{
				go = new GameObject ("Demo Map");
		
		
				//GameObject gridMap = new GameObject ("grid_map");
				//gridMap.transform.position = Vector3.zero;
				//var lineRenderer = gridMap.AddComponent<LineRenderer>();
				//lineRenderer.SetVertexCount ((int)xCount * (int)yCount);
				//lineRenderer.SetWidth (0.1f, 0.1f);
				//Instantiate (gridMap);
				go.transform.localScale = new Vector3 (xCount, 2.0f, yCount);
				go.transform.localPosition += Vector3.up * 2.0f;

				
		
				GameObject[] tileNodeCounts = new GameObject[(int)xCount * (int)yCount];
				nodeSize = tileSize;
				Vector3 tempNode = Vector3.zero;
				mapVertices = new Vector3[(int)xCount * (int)yCount];
				//markNode.SetActive (false);
				//Instantiate (markNode);
		
		
		
				int index = 0;
				for (int i=0; i<xCount; i++) {
			
						for (int j=0; j<yCount; j++) {
								float positionX = i * (nodeSize + xOffset);
								float positionY = j * (nodeSize + yOffset);
								tempNode = new Vector3 (positionX, 0.0f, positionY);
								allTileNodes.Add (tempNode, 0);
								Debug.Log ("tempNode[" + i + "," + j + "] = [" + tempNode.x + "," +
										"" + tempNode.y + "," + tempNode.z + "];");
								mapVertices [index] = tempNode;
								//find a pivot default has one vector3 in pivot set.
                                if (isDEBUG)
                                {
                                    if (i == xCount / 2 && j == yCount / 2)
                                    {
                                        mMapPivotSpace.item1 = tempNode;
                                        GameObject g = GameObject.CreatePrimitive(PrimitiveType.Cube);
                                        g.transform.parent = go.transform;
                                        g.transform.localPosition = mMapPivotSpace.item1;
                                        g.renderer.material.color = Color.green;
                                        g.transform.localScale = new Vector3(tileSize, tileSize + 2.5f, tileSize);
                                        mMapPivotSpace.item2 = tempNode;
                                        mMapPivotSpace.item3 = 1;
                                        mMapPivotSpace.item4 = 1;
                                    }
                                }
								index++;
								//Debug.Log ("x =" + tempNode.x + ",y=" + tempNode.z);
						}
		
				}

				adjustAnchorPosition (mMapPivotSpace.item1,1,1,true);

							
								
				//Debug.Log ("---------------------------------");
				//GameObject meshObject = GameObject.CreatePrimitive (PrimitiveType.Plane);
				//meshObject.name = "meshObject";
				//meshObject.transform.parent = go.transform;
				//meshObject.transform.localScale = meshObject.transform.parent.localScale;
				//		mf=meshObject.GetComponent<MeshFilter>();
				//		Mesh mesh = new Mesh ();
				//		mesh.vertices = allTileNodes.ToArray ();
				//		mf.sharedMesh = mesh;
				//		Vector3[] originVertices=mf.mesh.vertices;
				//		Vector3 size = new Vector3 (tileSize,0.0f,tileSize);
				//		for(int index2=0;index2<originVertices.Length;index2++){
				//			originVertices[index2]+=size;
				//		}
				//
				//		mesh.RecalculateBounds ();
		
				MeshFilter mf = go.AddComponent<MeshFilter> ();
				Mesh mesh = new Mesh ();
				mesh.vertices = mapVertices;
				mf.mesh = mesh;
				mesh.RecalculateBounds ();
		
				mr = go.AddComponent<MeshRenderer> ();
				//go.renderer.material.color = Color.blue;
				//mc=meshObject.GetComponent<MeshCollider>();
		
				//Vector3 center = Vector3.zero;
				//foreach(Vector3 v3 in allTileNodes){
				//	center+=v3;
				//}
		
				//Vector3 center = new Vector3 (mf.mesh.vertices.);
				//meshObject.transform.localPosition = center / allTileNodes.Count;
		
				//mr.material.color=Color.blue;
		
				//UnityEngine.Debug.Log ("allTileNodeCounts = " + allTileNodes.Count);
				CBlockMap defaultMap = new CBlockMap ();
				index = 0;
		    //t.SetActive (false);
                if (isDEBUG)
                {
                    foreach (Vector3 entry in mapVertices)
                    {
                        Vector3 currVet3 = Vector3.zero;
                        currVet3 = entry;
                        GameObject t = GameObject.CreatePrimitive(PrimitiveType.Cube);
                        //estimate the unit space
                        //markNode.transform.localScale = new Vector3 (tileSize+0.75f,tileSize*3,tileSize*+1.75f);
                        //markNode.renderer.material.color = Color.gray;
                        //lineRenderer.SetPosition(index,v);
                        t.name = "t_" + (int)currVet3.x + "_" + (int)currVet3.z;
                        //TileNode node = t.AddComponent<TileNode> ();
                        //node.parent=defaultMap;
                        t.transform.parent = go.transform;
                        t.transform.localPosition = new Vector3(currVet3.x, currVet3.y + 1.0f, currVet3.z);
                        t.transform.localScale = new Vector3(tileSize, 0.0f, tileSize);
                        t.renderer.material.color = Color.cyan;
                        index++;

                        //int currAvailableSpace = entry.Value;


                    }
                }
		    index = 0;
				foreach (Vector3 entry in mapVertices) {
				        
						Debug.Log (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>index=" + index + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						Vector3 currVet3 = Vector3.zero;
						tempRigidBody=null;
						currVet3 = entry;
						index++;
			
						//int currAvailableSpace = entry.Value;
						movePivotSpace = mPivotSpace;
			
						buildModel = searchPivotXY (markNode, go, currVet3);
						//markNode.transform.localScale = new Vector3 (tileSize,tileSize*5,tileSize);
						if (buildModel != null) {
								Debug.Log ("direction =" + direction);
								CharacterDisplayTemplate viewModel = buildModel.AddComponent<CharacterDisplayTemplate> ();
								mBoxCollider=buildModel.AddComponent<BoxCollider>();
								mBoxCollider.isTrigger=true;
								mBoxCollider.size=boxScale;
						        buildModel.tag = buildingCollideFlag;
						        buildModel.layer = buildingLayer;   
								//add rigid body in it
				                tempRigidBody=buildModel.AddComponent<Rigidbody>();
				                tempRigidBody.useGravity=false;
                               // tempRigidBody
								tempRigidBody.isKinematic=true;	
								tempRigidBody.freezeRotation=true;	
								viewModel.mTileSize = tileSize;
								viewModel.mAllTileNodes = allTileNodes;
								viewModel.xMapCount = xCount;
								viewModel.yMapCount = yCount;
								viewModel.segmentWidthTileCount = segmentWidthTileCount;
								viewModel.segmentHeightTileCount = segmentHeightTileCount;
								//reset currVet3
								currVet3 = movePivotSpace.item2;
								Debug.Log ("currVet3 =[" + currVet3.x + "," + currVet3.y + "," + currVet3.z + "];");
								switch (direction) { 
					
								//right
								case 1:
					//Vector3 movePivotX = movePivotSpace.item1;
					
										for (int i=0; i<segmentWidthTileCount; i++) {
												for (int j=0; j<segmentHeightTileCount; j++) {
														currVet3 = movePivotSpace.item2 - new Vector3 (i * (tileSize + xOffset), 0.0f, j * (tileSize + yOffset));
														allTileNodes.Remove (currVet3);
														allTileNodes.Add (currVet3, 1);
														Debug.Log ("marked used recently: =[" + currVet3.x + "," + currVet3.y + "," + currVet3.z + "];");
							
												}
										}
										mPivotSpace = movePivotSpace;
					
										break;
								}
								// allTileNodes
                                buildModel.AddComponent<DragAndDrop>();
						}
						//break;
				}


				index = 0;
				foreach (KeyValuePair<Vector3,int> entry in allTileNodes) {
			
						Debug.Log ("Finally filter: index =" + index + "; " + "" +
								"key=" + "[" + entry.Key.x + "," + entry.Key.y + "" +
								"," + entry.Key.z + "" +
								"]" + "; value =" + entry.Value);
						index++;
				}
		
				//		Mesh mesh = new Mesh ();
				//		int[] triangles=new int[6];
				//		mesh.vertices = new Vector3[]{new Vector3(0.0f,0.5f,0.5f)
				//			,new Vector3(0.0f,0.0f,0.5f),
				//			new Vector3(0.5f,0.5f,0.5f),
				//			new Vector3(0.0f,0.5f,0.0f)
				//		};
				//		triangles[0]=0;
				//		triangles[1]=1;
				//		triangles[2]=3;
				//		triangles[3]=0;
				//		triangles[4]=2;
				//		triangles[5]=3;
				//		mesh.triangles=triangles;
				//
				//		mf.mesh = mesh;
		}
	


	public void adjustAnchorPosition (Vector3 target,int width,int height,bool isNew)
	{
		int index = 0;
		string label = "";
		int chunk = 0;
		for (int p = 0; p < 2; p++) {
			if(isNew){
			mAnchors [index] = GameObject.CreatePrimitive (PrimitiveType.Cube);
			mAnchors [index].transform.parent = go.transform;
			mAnchors [index].transform.localScale = new Vector3 (tileSize / 3, tileSize + 2.0f, tileSize / 3);
			mAnchors [index].renderer.enabled = true;
			mAnchors [index].renderer.material.color = Color.gray;
			mAnchors [index].name = "anchor_" + (label = (p == 0) ? "left" : "right");
			}
			mAnchors [index].transform.localPosition = Vector3.zero+new Vector3 (target.x + (chunk=(p==0)?-2:(width+1)) * ((tileSize + xOffset)/2), target.y + 0.7f, target.z);
			index++;
		}
		/*if (!isNew) {
			mAnchors [index-1].transform.localPosition = Vector3.zero+new Vector3 (target.x + width+1 * ((tileSize + xOffset)/2), target.y + 0.7f, target.z);

		}*/

		for (int k = 0; k < 2; k++) {
			if(isNew){
			mAnchors [index] = GameObject.CreatePrimitive (PrimitiveType.Cube);
			mAnchors [index].transform.parent = go.transform;
			mAnchors [index].transform.localScale = new Vector3 (tileSize / 3, tileSize + 2.0f, tileSize / 3);
			mAnchors [index].renderer.enabled = true;
			mAnchors [index].renderer.material.color = Color.gray;
			mAnchors [index].name = "anchor_" + (label = (k == 0) ? "down" : "up");
			}
			mAnchors [index].transform.localPosition = new Vector3 (target.x, target.y + 0.7f, target.z + (chunk=(k==0)?-3:height+1) * ((tileSize + yOffset)/2));
			index++;
		}

	}

	public void adjustAnchorPosition2 (Vector3 target,int width,int height,bool isNew)
	{
		int index = 0;
		string label = "";
		float chunk = 0;
		for (int p = 0; p < 2; p++) {
			mAnchors [index].transform.localPosition = Vector3.zero+new Vector3 (target.x + (chunk=(p==0)?-2-0.25f:(width+1.5f)) * ((tileSize + xOffset)/2)-tileSize/2, target.y + 0.7f, target.z);
			Debug.Log ("----------------------"+index+"-----------------------------------------------");

			index++;
		}
		/*if (!isNew) {
			mAnchors [index-1].transform.localPosition = Vector3.zero+new Vector3 (target.x + width+1 * ((tileSize + xOffset)/2), target.y + 0.7f, target.z);

		}*/
		//if(!isNew)
		//	mAnchors[1].transform.localPosition=Vector3.zero+new Vector3 (2.3f,0.78f,8.1f);
		//index = 2;
		for (int k = 0; k < 2; k++) {
			mAnchors [index].transform.localPosition = new Vector3 (target.x, target.y + 0.7f, target.z + (chunk=(k==0)?-2-0.25f:height+1.5f) * ((tileSize + yOffset)/2)-tileSize/2);
			Debug.Log ("----------------------"+index+"-----------------------------------------------");

			index++;
		}
	}





	float moveSpeed = 2f;
	
		public void Update ()
		{
				if (Input.GetKey (KeyCode.LeftArrow)) {
						Vector3 v = Vector3.left * (transform.position.x) * Time.deltaTime * moveSpeed;
						if (Mathf.Abs (v.x) > 5) {
								v.x = 5.0f;
						}
						transform.position += v;
						return;
				}
		}
	
		GameObject searchPivotXY (GameObject markNode, GameObject parent, Vector3 currVet3)
		{
				buildModel = null;
				markNode = null;
				mPivotSpace.item1 = new Vector3 (mPivotSpace.item1.x, 0.0f, 0.0f);
				mPivotSpace.item2 = new Vector3 (mPivotSpace.item1.x + segmentWidthTileCount * (tileSize + xOffset), 0.0f, mPivotSpace.item1.z + segmentHeightTileCount * (tileSize + xOffset));
				mPivotSpace.item3 = (int)segmentWidthTileCount;
				mPivotSpace.item4 = (int)segmentHeightTileCount;
				Debug.Log ("----------------ORIGIN----------------mPivotSpace.item1 = [" + mPivotSpace.item1.x + "," +
						"" + mPivotSpace.item1.y + "," + mPivotSpace.item1.z + "];" +
						"mPivotSpace.item2 =[" + mPivotSpace.item2.x + "," + mPivotSpace.item2.y + "," + mPivotSpace.item2.z);
				//Debug.Log ("mPivotSpace.value1.x =" + mPivotSpace.item1.x);
				if (mPivotSpace.item1.x < allTileNodes.Count - 1) {
						if (isDefaultOccupySpceByCharacter (movePivotSpace, allTileNodes, (int)xCount, (int)yCount)) {
								Debug.Log ("Instiate and now");
								markNode = Object.Instantiate (Resources.Load ("FBX/ShouLan")) as GameObject;
								markNode.name = "_character_" + (int)currVet3.x + "_" + (int)currVet3.z;
								markNode.tag="character_"+modelType;
								markNode.transform.parent = parent.transform;
								//markNode.transform.localPosition = new Vector3 (currVet3.x + 1, currVet3.y + 1.0f, currVet3.z + 1);
								//Debug.Log ("(movePivotSpace.item2.z)/2+segmentHeightTileCount*yOffset =" + (movePivotSpace.item2.z) / 2);
								markNode.transform.localPosition = new Vector3 (movePivotSpace.item1.x + ((segmentWidthTileCount - 1) * (tileSize + yOffset)) / 2, movePivotSpace.item1.y + 3.0f, movePivotSpace.item1.z + ((segmentHeightTileCount - 1) * (tileSize + yOffset)) / 2);
								//markNode.transform.localScale = new Vector3 ((segmentWidthTileCount-1)*(tileSize+xOffset)+tileSize, 0.2f, (segmentHeightTileCount-1)*(tileSize+yOffset)+tileSize);
				
								//Debug.Log ("mPivotSpace.item.x =" + mPivotSpace.item1.x + ",mPivotSpace.item1.z =" + mPivotSpace.item1.z + ";");
				
								//range
								GameObject rangeScopeGM = GameObject.CreatePrimitive (PrimitiveType.Cube);
				
								rangeScopeGM.name = "rang" + (int)currVet3.x + "_" + (int)currVet3.z;
								//TileNode node = t.AddComponent<TileNode> ();
								//node.parent=defaultMap;
								rangeScopeGM.transform.parent = parent.transform;
								rangeScopeGM.transform.localPosition = new Vector3 (movePivotSpace.item1.x + ((segmentWidthTileCount - 1) * (tileSize + yOffset)) / 2, movePivotSpace.item1.y + 1.8f, (movePivotSpace.item1.z + ((segmentHeightTileCount - 1) * (tileSize + yOffset)) / 2));
								//t.transform.localPosition = new Vector3 (movePivotSpace.item1.x, movePivotSpace.item1.y + 3.0f, movePivotSpace.item1.z);
								rangeScopeGM.transform.localScale = new Vector3 ((segmentWidthTileCount - 1) * (tileSize + xOffset) + tileSize, 0.2f, (segmentHeightTileCount - 1) * (tileSize + yOffset) + tileSize);
								rangeScopeGM.renderer.material.color = Color.green;

								//left
								//anchor.transform.parent=parent.transform;
								//anchor.transform.position=
								//right

								//upward

								//downward

								buildModel = new GameObject ();
								buildModel.name = "Build_Model" + currVet3.x + "_" + (int)currVet3.z;
						        buildModel.tag = buildingCollideFlag;

								buildModel.transform.parent = parent.transform;
								buildModel.transform.localPosition = new Vector3 (movePivotSpace.item1.x + ((segmentWidthTileCount - 1) * (tileSize + yOffset)) / 2, movePivotSpace.item1.y + 3.3f, movePivotSpace.item1.z + ((segmentHeightTileCount - 1) * (tileSize + yOffset)) / 2);
								buildModel.transform.localScale=rangeScopeGM.transform.localScale;
				               
								markNode.transform.parent = buildModel.transform;
								rangeScopeGM.transform.parent = buildModel.transform;
								Debug.Log ("buildModel position:[" + buildModel.transform.position.x + "," + buildModel.transform.position.y + "," + buildModel.transform.position.z + "].");
								return buildModel;
						} 
						searchPivotXY (markNode, parent, currVet3); 
				}
				return null;
		}
	
		bool isDefaultOccupySpceByCharacter (CQuad<Vector3,Vector3,int,int> pivotSpace, Dictionary<Vector3,int> allTileNodes, int xCount, int yCount)
		{
				//reset unit space
				//occupy space by current character 
				Vector3 pivotHorizontal = pivotSpace.item1;
				Vector3 pivotVertical = pivotSpace.item2;
				int xSegmentWidth = pivotSpace.item3;
				int ySegmentHeight = pivotSpace.item4;
				int currAvailabe = 1;
				int countX = xSegmentWidth;
				int countY = ySegmentHeight;
				//Debug.Log ("xSegmentWidth =" + xSegmentWidth);
				int step = 1;
				bool isDefault = false;
				Vector3 vX = Vector3.zero;
				Vector3 vY = Vector3.zero;
				movePivotSpace.item3 = xSegmentWidth;
				movePivotSpace.item4 = ySegmentHeight;
				{
						//whether vector3 is unavailable
						//allTileNodes.TryGetValue (pivot, out currAvailabe);
			
			
			
			
			
			
			
						direction = 1;
						switch (direction) {
						//move target 
						//left direction
				
						case 0:
								vX.x = -step * (tileSize + xOffset);
								vY.x = -step * (tileSize + xOffset);
				
								vY.z = step * (countY * (tileSize + yOffset));
								pivotHorizontal += vX;
								pivotVertical += vY;
								movePivotSpace.item1 = pivotHorizontal;
								movePivotSpace.item2 = pivotVertical;
								currAvailabe = estimateAvailableSpace (movePivotSpace, currAvailabe, allTileNodes);
				
								break;
						case 1:
				//right direction
								vX.x = step * (tileSize + xOffset);
								vY.x = step * (tileSize + xOffset);
								pivotHorizontal += vX;
								pivotVertical += vY;
								movePivotSpace.item1 = pivotHorizontal;
								movePivotSpace.item2 = pivotVertical;
								Debug.Log ("----------------MOVING----------------movePivotSpace.item1 = [" + mPivotSpace.item1.x + "," +
										"" + movePivotSpace.item1.y + "," + movePivotSpace.item1.z + "];" +
										"movePivotSpace.item2 =[" + movePivotSpace.item2.x + "," + movePivotSpace.item2.y + "," + movePivotSpace.item2.z + "]");
								currAvailabe = estimateAvailableSpace (movePivotSpace, currAvailabe, allTileNodes); 
								break;
						case 2:
				//forward direction
								vX.z = step * (tileSize + yOffset);
								vY.x = step * (countX * (tileSize + xOffset));
								vX.z = step * (tileSize + yOffset);
								pivotHorizontal += vX;
								pivotVertical += vY;	
								movePivotSpace.item1 = pivotHorizontal;
								movePivotSpace.item2 = pivotVertical;
								currAvailabe = estimateAvailableSpace (pivotSpace, currAvailabe, allTileNodes);
				
								break;
						//downward directon
						case 3:
								vX.z = -step * (tileSize + yOffset);
								vY.x = step * (countX * (tileSize + xOffset));
								vX.z = -step * (tileSize + yOffset);
								pivotHorizontal += vX;
								pivotVertical += vY;	
								movePivotSpace.item1 = pivotHorizontal;
								movePivotSpace.item2 = pivotVertical;
								currAvailabe = estimateAvailableSpace (pivotSpace, currAvailabe, allTileNodes);
				
								break;
						default:
								currAvailabe += 1;
								break;
						}
				}
				Debug.Log ("currAvailabe ===========" + currAvailabe);
				if (currAvailabe == 1) {
			
						isDefault = true;		
				}
				return isDefault;
		}
	
		int estimateAvailableSpace (CQuad<Vector3,Vector3,int,int> pivotSpace, int currAvailabe, Dictionary<Vector3,int> allTileNodes)
		{          
				int _ca = 1;
				Vector3 pivotHorizontal = pivotSpace.item1;
				Vector3 pivotVertical = pivotSpace.item2;
				int xSegmentWidth = pivotSpace.item3;
				int ySegmentHeight = pivotSpace.item4;
				Vector3 segmentSpace = Vector3.zero;
		
				Vector3 currX = pivotHorizontal;
				Vector3 currY = pivotVertical;
				Debug.Log ("pivotHorizontal =[" + pivotHorizontal.x + "," + pivotHorizontal.y + "," + pivotHorizontal.z + "];" +
						"pivotVertical =[" + pivotVertical.x + "," +
						"" + pivotVertical.y + "," +
						"" + pivotVertical.z + "].");
				//Debug.Log ("xCount / 2=" + xCount / 2 + ";xSegmentWidth =" + xSegmentWidth + "; ySegmentHeight =" + ySegmentHeight + "; yCount / 2 =" + yCount / 2);
				if (currAvailabe != 0) { 
						if ((xSegmentWidth > 1 && xSegmentWidth < xCount / 2) && (ySegmentHeight > 1 && ySegmentHeight < yCount / 2)) {
								//if (xSegmentWidth < xCount / 2 || ySegmentHeight < yCount / 2) {
				
								//for (int i=0; i<xSegmentWidth; i++) {
								//	for (int j=0; j<ySegmentHeight; j++) {
								//		allTileNodes.Remove (curr);
				
				
								//	}
								//}
								Debug.Log ("-----------------I'm coming 1-----------");
								if (allTileNodes.ContainsKey (currX) && allTileNodes.ContainsKey (currY)) {
										allTileNodes.TryGetValue (currX, out currAvailabe);
										_ca += currAvailabe;
										allTileNodes.TryGetValue (currY, out currAvailabe);
										_ca += currAvailabe;
										string a = (_ca == 1) ? "Available" : "UnAvailable";
										Debug.Log ("currX and currY is " + a + "------------1------------");
										return _ca;
								}
				
						}
						if ((xSegmentWidth > 1 && xSegmentWidth < xCount / 2) || (ySegmentHeight > 1 && ySegmentHeight < yCount / 2)) {
								//if (xSegmentWidth < xCount / 2 || ySegmentHeight < yCount / 2) {
								Debug.Log ("----------------I'm coming 2------------------");
								if (allTileNodes.ContainsKey (currX) && allTileNodes.ContainsKey (currY)) {
					
										allTileNodes.TryGetValue (currX, out currAvailabe);
										_ca += currAvailabe;
										allTileNodes.TryGetValue (currY, out currAvailabe);
										_ca += currAvailabe;
										string a = (_ca == 1) ? "Available" : "UnAvailable";
										Debug.Log ("currX and currY is " + a + "------------2------------");
										return _ca;
								}
						}
				}
		
				return currAvailabe += 1;
		}
		//	bool isEnableDrawIcon=true;
		//	public void OnDrawGizmos(){
		//		Gizmos.color = new Color(1, 0, 0, 0.5F);
		//
		//		if (isEnableDrawIcon) {
		//		if(allTileNodes.Count>0){
		//			foreach(Vector3 v in allTileNodes){
		//				UnityEngine.Debug.Log("tempNode("+v.x+","+v.y+","+v.z);
		//				Gizmos.DrawIcon(v, "Light Gizmo.tiff", true);
		//			}
		//			isEnableDrawIcon=false;
		//			}   
		//		}
		//	}
	
}
